import { ErrorMessageMode } from '/#/axios';
import { defHttp } from '/@/utils/http';
import type { CreateDeck, ModifyDeck, DeckDetail, DeckList, DeckListQuery } from './model/deckModel';

enum Api {
  DeckList = '/tool/dnm/deck-list',
  DeckDetail = '/tool/dnm/deck-info',
  ModifyDeck = '/tool/dnm/update-deck',
  CreateDeck = '/tool/dnm/create-deck',
  DeleteDeck = '/tool/dnm/delete-deck',
  ExportDeckImg = '/tool/dnm/export-deck-img',
}

// 获取套牌列表
export function DeckListApi(params: DeckListQuery, model: ErrorMessageMode = 'dialog') {
  return defHttp.post<DeckList>(
    {
      url: Api.DeckList,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 获取套牌列表
export function DeckDetailApi(params: Pick<DeckDetail, 'id'>, model: ErrorMessageMode = 'dialog') {
  return defHttp.post<DeckDetail>(
    {
      url: Api.DeckDetail,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

export function ModifyDeckApi(params: ModifyDeck, model: ErrorMessageMode = 'dialog') {
  const jsonParam = {
    id: params.id,
    deck_name: params.deck_name,
    deck_info: params ? JSON.stringify(params) : '{}',
  };
  return defHttp.post(
    {
      url: Api.ModifyDeck,
      data: jsonParam,
    },
    {
      errorMessageMode: model,
    }
  );
}

export function CreateDeckApi(params: CreateDeck, model: ErrorMessageMode = 'dialog') {
  const jsonParam = {
    deck_name: params.deck_name,
    deck_info: params ? JSON.stringify(params) : '{}',
  };
  return defHttp.post(
    {
      url: Api.CreateDeck,
      data: jsonParam,
    },
    {
      errorMessageMode: model,
    }
  );
}

export function DeleteDeckApi(params: number[], model: ErrorMessageMode = 'notify') {
  const jsonParam = {
    deck_ids: params.join(','),
  };
  return defHttp.post(
    {
      url: Api.DeleteDeck,
      data: jsonParam,
    },
    {
      errorMessageMode: model,
    }
  );
}

export function ExportDeckImgApi(params: Pick<DeckDetail, 'id'>, model: ErrorMessageMode = 'dialog') {
  return defHttp.post<string>(
    {
      url: Api.ExportDeckImg,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}
